//
//  MainMenuViewController.m
//  KG
//
//  Created by Eugene Braginets on 14.03.12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MainMenuViewController.h"
#import "AppDelegate.h"
#import "StartScreenViewController.h"
#import "FixturesResultViewController.h"
#import "PitchLocatorViewController.h"
#import "LatestNewsviewController.h"
#import "StandartContentViewController.h"
#import "EventsCalendarViewController.h"
#import "CountyPlayersViewController.h"
#import "LottoViewController.h"
#import "cellView.h"
#import "bannerView.h"


@implementation MainMenuViewController
@synthesize tableView=_tableView;
@synthesize myCell=_myCell;
@synthesize bannerViewController=_bannerViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    
        

    }
    return self;
   
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [self.navigationItem setTitle:@"MAIN MENU"];
    
    
    bannerView *bannerVC=[[bannerView alloc] init];
    [self.view addSubview:bannerVC.view];
    
    [bannerVC animate];
    self.bannerViewController=bannerVC;

    
    
//    UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] 
//                                     initWithTitle:@"More"                                            
//                                     style:UIBarButtonItemStyleBordered 
//                                     target:self 
//                                     action:@selector(more)];
//    self.navigationItem.rightBarButtonItem = logoutButton;
//    [self.view setBackgroundColor:[UIColor colorWithRed:.3 green:.6 blue:.1 alpha:1]];


   
//    [self fb];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


- (void) fb {

}


- (void)viewWillAppear:(BOOL)animated {
  
    
    [self setTitle:@"Main Menu"];
    NSLog(@"View will appear!");
    
    [super viewWillAppear:animated];
    
}

- (void) viewDidDisappear:(BOOL)animated {

    [super viewDidDisappear:animated];
    
}

- (void)viewDidUnload
{
    [self setBannerViewController:nil];

    [self setTableView:nil];
    [self setMyCell:nil];
    
    
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:kNewsItem
                                                  object:nil]; 

    
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Custom Methods

- (void) more {
    NSLog(@"more");
}


#pragma mark - TableView  delegate

// Customize the number of sections in the table view.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return floor(357/[kItems count]);    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [kItems count];
}



// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *MyIdentifier = @"MyIdentifier";
    MyIdentifier = @"tblCellView";
    
    cellView *cell = (cellView*)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if(cell == nil) {
        [[NSBundle mainBundle] loadNibNamed:@"TableCellView" owner:self options:nil];
        cell=self.myCell;
    }
    
	cell.cellTitle.text=[kItems objectAtIndex:indexPath.row];
    cell.cellSubTitle.text=[kSubtitles objectAtIndex:indexPath.row];

    
    [cell setBackgroundColor:[UIColor clearColor]];
    
    
    UIView *bgColorView = [[UIView alloc] init];
    [bgColorView setBackgroundColor:[UIColor colorWithRed:.25 green:.3 blue:.05 alpha:0.7]];
    

    
    [cell setSelectedBackgroundView:bgColorView];
    
    [cell.cellTitle setHighlightedTextColor:[UIColor whiteColor]];
    [cell.cellSubTitle setHighlightedTextColor:[UIColor whiteColor]];
    [cell.leftImage setHighlightedImage:[UIImage imageNamed:@"shevron.png"]];
    return cell;
}



//- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    //    if (cell.detailTextLabel.text==[ktypes objectAtIndex:1])
//    //       [cell setBackgroundColor:[UIColor colorWithRed:.8 green:.8 blue:1 alpha:1]];
//    //    if (cell.detailTextLabel.text==[ktypes objectAtIndex:2])
//    //        [cell setBackgroundColor:[UIColor colorWithRed:1 green:.8 blue:.8 alpha:1]];
//    
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog (@"selected row %d", indexPath.row);
   [self setTitle:@"Back"];
    switch (indexPath.row) {
        case 0: {
                [myAppDelegate getData:kGetNewsURL
                      andType:kNewsItem];
            
            LatestNewsviewController* viewController=[[LatestNewsviewController alloc] init];
            viewController.navigationItem.title=[kItems objectAtIndex:indexPath.row];
            
            [self.navigationController pushViewController:viewController animated:YES];
        
            break;
        }    
        case 1: {
            FixturesResultViewController* viewController=[[FixturesResultViewController alloc] init];
            viewController.navigationItem.title=[kItems objectAtIndex:indexPath.row];
            
            [self.navigationController pushViewController:viewController animated:YES];
            
            break;
        }
        case 2: {
            
            [myAppDelegate getData:kGetPitchesURL
                      andType:kPitchItem];
            
            PitchLocatorViewController* viewController=[[PitchLocatorViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            
            break;
        }    
            
        case 3: {
            
            [myAppDelegate getData:kGetEventURL
                  andType:kEventItem];
            
            EventsCalendarViewController* viewController=[[EventsCalendarViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            
            break;
        }    
        case 4: {
            
             [myAppDelegate getData:kGetPlayersURL
               andType:kPlayerItem];
            
            CountyPlayersViewController* viewController=[[CountyPlayersViewController alloc] init];
            [self.navigationController pushViewController:viewController animated:YES];
            
            break;
        }    
        case 5: {
            LottoViewController* viewController=[[LottoViewController alloc] init];
            viewController.navigationItem.title=@"Lotto Results";
            [self.navigationController pushViewController:viewController animated:YES];
            
            break;
        }    
        default:
            break;
    }
    
   

}

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
    NSLog (@"selected accessory from row %d", indexPath.row);
}


#pragma mark - Navigation Controller Delegate

- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewWillAppear:animated];
}

- (void)navigationController:(UINavigationController *)navigationController 
       didShowViewController:(UIViewController *)viewController animated:(BOOL)animated 
{
    [viewController viewDidAppear:animated];
}


@end
